import re
import time
from selenium.webdriver.common.by import By as by
from Page.UserPage.ErpLoginPage import ErpLoginPage


class PurchaseApplyPage(ErpLoginPage):
    """表现层"""

    # 采购管理
    purchase_button = (by.XPATH,'//div[@class="ant-layout-sider-children"]/ul/li[3]')

    # 请购单
    purchase_apply = (by.CSS_SELECTOR,'a[target="请购单"]')

    # 新增请购单
    new_add = (by.CSS_SELECTOR,'.table-operator>button:nth-child(1)')

    # 关闭引导
    skip_button = (by.CSS_SELECTOR,'.introjs-skipbutton')

    # 点击选择商品
    find_goods = (by.CSS_SELECTOR,'button[class="ant-btn ant-btn-icon-only"]') # 之前写的是(by.CLASS_NAME，'ant-btn ant-btn-icon-only')

    # 选中商品
    select_goods = (by.XPATH,'(//tbody[@class="ant-table-tbody"])[2]//input[@type="checkbox" and @class="ant-checkbox-input"]')

    # 点击确定
    determine = (by.CSS_SELECTOR,'.ant-btn-primary:nth-child(2)')

    # 保存并审核
    save_examine = (by.CSS_SELECTOR,'.ant-modal-footer>.ant-btn:nth-child(3)')

    # 数据量
    data_num = (by.CLASS_NAME,'ant-pagination-total-text')

    """操作层"""

    def click_purchase_apply(self):
        # 进入到请购单页面
        self.click(self.purchase_button)
        time.sleep(1)
        self.click(self.purchase_apply)

    def click_select_goods(self):
        """点击新增并选择商品"""
        self.click(self.new_add)
        self.click(self.skip_button)
        time.sleep(1)
        self.click(self.find_goods)
        self.click(self.select_goods)

    def click_determine(self):
        """点击确定"""
        self.click(self.determine)

    def click_save_examine(self):
        """点击保存并审核"""
        self.click(self.save_examine)
        # self.refresh()

    def get_data_num(self):
        """获取数据数量"""
        text = self.get_element_text(self.data_num)  # 获取文本
        text = text[4:] # 切片
        num = int(re.findall(r'\d+', text)[0]) # 正则表达式？
        return num


if __name__ == '__main__':
    pa = PurchaseApplyPage()
    pa.common_login()
    pa.click_purchase_apply()
    pa.click_select_goods()
    pa.click_determine()
    pa.click_save_examine()
    pa.get_data_num()
